package com.spark.demo;

import java.util.Arrays;

/**
 * @Author sc
 * @DATE 2024/5/28 20:29
 * @Description:
 * @Version 1.0
 */
public class Demo2 {

    public static void main(String[] args) {
        int[] nums = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99}; // 示例数组
        int target = 84; // 目标值
        int result = searchInsertPosition(nums, target);
        System.out.println("目标值 " + target + " 在数组中的最小索引为: " + result);



        int[] nums2 = {1,1,2};
        System.out.println(removeDuplicates(nums2));


        int [] arr2 = {1,3,5,6};
        System.out.println(searchInsert(arr2, 7));

        char [] s = {'1','3','5','6'};
        reverseString(s);
        System.out.println(Arrays.toString(s));

        System.out.println();

        String sa = "aacdaaooaa";
        System.out.println(maximumLength(sa));
    }

    public static int searchInsertPosition(int[] nums, int target) {
        Arrays.sort(nums); // 对数组进行排序

        // 二分查找目标值
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        // 如果未找到目标值，则返回它将被按顺序插入的位置的索引
        return left;
    }

    public static int removeDuplicates(int[] nums) {
        int i = 0;
        for(int j=1;j<nums.length;j++){
            if(nums[i] != nums[j]){
                nums[++i] = nums[j];
            }
        }
        return i+1;
    }

    /**
     *  二分法查找
     * @param nums
     * @param target
     * @return
     */
    public static int searchInsert(int[] nums, int target) {

        int left = 0;
        int right = nums.length - 1;

        while(left <= right){
            int mid = (left + right) / 2;
            if(nums[mid] < target){
                left = mid + 1;
            }else if(nums[mid] > target){
                right = mid - 1;
            }else{
                return mid;
            }
        }

        return left;

    }

    /**
     *  反转字符串
     * @param s
     */
    public static void reverseString(char[] s) {
        int len = s.length;
        int size = len;
        for(int i = 0;i < len/2;i++){
            size--;
            char tmp = s[i];
            s[i] = s[size];
            s[size] = tmp;
        }
    }

    /**
     *  如果一个字符串仅由单一字符组成，那么它被称为 特殊 字符串。
     *  例如，字符串 "abc" 不是特殊字符串，而字符串 "ddd"、"zz" 和 "f" 是特殊字符串。
     *  返回在 s 中出现 至少三次 的 最长特殊子字符串 的长度，如果不存在出现至少三次的特殊子字符串，则返回 -1
     * @param s
     * @return
     */
    public static int maximumLength(String s) {
        int[][] counts = new int[26][s.length()];
        int result = -1;
        for(int i = 0; i < s.length();i++){
            for(int j = i; j < s.length();j++){
                if(s.charAt(i) != s.charAt(j)){
                    break;
                }
                if(++counts[s.charAt(i) - 'a'][j - i] >= 3){
                    result = Math.max(result,j - i);
                    System.out.println();
                    System.out.println(Arrays.deepToString(counts));
                }
            }
        }
        return result == -1?-1:result + 1;
    }

}
